package com.zerui.manager.devicegroup;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;
import lombok.experimental.Accessors;

import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.Set;

/**
 * @Author wh
 * @create_time 2023-08-23 11:07
 */
@ApiModel("设备分组")
@Getter
@ToString
@Accessors(chain = true)
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class DeviceGroup {

	@ApiModelProperty("主键，无业务含义")
	@TableId(value="id",type= IdType.ASSIGN_ID)
	@JsonFormat(shape = JsonFormat.Shape.STRING)
	private Long id;

	@ApiModelProperty("分组名称")
	private String orgName;

	@ApiModelProperty("上级id")
	private Long supId;

	@ApiModelProperty("绑定设备")
	@TableField(value = "device_ids",typeHandler = JacksonTypeHandler.class)
	private Set<String> deviceIds;

	@ApiModelProperty("创建时间")
	@JsonFormat(shape = JsonFormat.Shape.STRING,pattern ="yyyy-MM-dd HH:mm:ss",timezone ="GMT+8")
	private LocalDateTime createTime;

	@ApiModelProperty("更新时间")
	@JsonFormat(shape = JsonFormat.Shape.STRING,pattern ="yyyy-MM-dd HH:mm:ss",timezone ="GMT+8")
	private LocalDateTime updateTime;

	public static DeviceGroup create(Long id,String orgName,Long supId,Set<String> deviceIds,LocalDateTime createTime,LocalDateTime updateTime){
		DeviceGroup deviceGroup = new DeviceGroup();
		deviceGroup.id = id;
		deviceGroup.orgName = orgName;
		deviceGroup.supId = supId;
		if(null == deviceIds){
			deviceIds = new HashSet<>(0);
		}
		if(null == createTime){
			createTime = LocalDateTime.now();
		}
		if(null == updateTime){
			updateTime = LocalDateTime.now();
		}
		deviceGroup.deviceIds = deviceIds;
		deviceGroup.createTime = createTime;
		deviceGroup.updateTime = updateTime;
		return deviceGroup;
	}

	public static final String ROOT_NODE_NAME = "根节点";

	public static Boolean isRootName(String name){
		return ROOT_NODE_NAME.equals(name);
	}

	public void setDeviceIds(Set<String> deviceIds){
		this.deviceIds = deviceIds;
	}

}
